package my.senix.sc.config;

import my.senix.sc.controller.SimpleController;
import my.senix.sc.controller.ValidationController;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * Created by lhs on 2018/11/25.
 */
@Component
public class MyNonWebStartup implements CommandLineRunner,ApplicationContextAware {

    private Environment environment;

    private ApplicationContext context;

    private StringRedisTemplate redisTemplate;


    public MyNonWebStartup(Environment environment,StringRedisTemplate redisTemplate){
        this.environment=environment;
        this.redisTemplate=redisTemplate;
    }

    @Override
    public void run(String... args) throws Exception {

        System.out.println("redis: "+redisTemplate.opsForValue().get("name"));

        redisTemplate.opsForValue().set("addr","yangxin");

        System.out.println("env:"+ Arrays.toString(environment.getActiveProfiles()));


        SimpleController simpleController = (SimpleController) context.getBean("simpleController");
        System.out.println("simpleController.hello(): "+ simpleController.hello("afweqfewqe"));


        ValidationController validateController = context.getBean(ValidationController.class);
//        System.out.println("validateController.age(): "+validateController.getAge(-1,""));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
